package com.rk.user.api;

import com.rk.common.result.Result;
import com.rk.common.util.AuthContextHolder;
import com.rk.hosp.model.user.Patient;
import com.rk.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author :Rk.
 * @date : 2022/11/26
 */
@RestController
@RequestMapping("/api/user/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;


    /**
     * 查询就诊人信息
     * @param request
     * @return
     */
    @GetMapping("/auth/findAll")
    public Result findAll(HttpServletRequest request) {
        //获取当前登录用户的id
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> list = patientService.findAllUserId(userId);

        return Result.ok(list);

    }


    /**
     * 添加用户
     * @param patient
     * @param request
     * @return
     */
    @PostMapping("/auth/save")
    public Result savePatient(@RequestBody Patient patient,HttpServletRequest request) {
        //获取当前登录用户的id
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }


    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("/auth/get/{id}")
    public Result findById(@PathVariable Long id) {
        //获取当前登录用户的id
        Patient patient = patientService.getPatientById(id);
        return Result.ok(patient);
    }


    /**
     * 通过id修改
     * @param patient
     * @return
     */
    @PostMapping("/auth/update")
    public Result update(@RequestBody Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }

    /**
     * 通过id删除
     * @param
     * @return
     */
    @DeleteMapping("/auth/remove/{id}")
    public Result remove(@PathVariable Long id){
        patientService.removeById(id);
        return Result.ok();
    }

    /**
     * 通过id获取就诊人
     * @param id
     * @return
     */
    @GetMapping("/inner/get/{id}")
    public Patient getByid(@PathVariable Long id){
        return patientService.getById(id);
    }

}
